package com.zdp.common.util;

import com.zdp.common.constant.ApiConstant;
import com.zdp.common.domain.ResultInfo;

/**
 * @author sesshomaru
 * @date 2021/7/30
 */
public class ResultInfoUtil {

    /**
     * 请求出错返回
     * @param <T>
     * @return
     */
    public static <T> ResultInfo<T> buildError() {
        ResultInfo<T> resultInfo = build(ApiConstant.ERROR_CODE,
                ApiConstant.ERROR_MESSAGE, null);
        return resultInfo;
    }

    public static <T> ResultInfo<T> buildError(String msg) {
        ResultInfo<T> resultInfo = build(ApiConstant.ERROR_CODE,
                msg, null);
        return resultInfo;
    }

    public static <T> ResultInfo<T> buildError(Integer status, String msg) {
        ResultInfo<T> resultInfo = build(status,
                msg, null);
        return resultInfo;
    }

    public static <T> ResultInfo<T> buildError(T data) {
        ResultInfo<T> resultInfo = build(ApiConstant.ERROR_CODE,
                ApiConstant.ERROR_MESSAGE, data);
        return resultInfo;
    }

    /**
     * 请求成功返回
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultInfo<T> buildSuccess(T data) {
        ResultInfo<T> resultInfo = build(ApiConstant.SUCCESS_CODE,
                ApiConstant.SUCCESS_MESSAGE, data);
        return resultInfo;
    }

    public static <T> ResultInfo<T> build(Integer status, String msg, T data) {
        if (status == null) {
            status = ApiConstant.SUCCESS_CODE;
        }
        if (msg == null) {
            msg = ApiConstant.SUCCESS_MESSAGE;
        }
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setStatus(status);
        resultInfo.setMsg(msg);
        resultInfo.setData(data);
        return resultInfo;
    }
}
